#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>


#define IMX6ULLIRQ_CNT		1			/* 设备号个数 */
#define IMX6ULLIRQ_NAME		"imx6ullirq"	/* 名字 */
#define KEY0VALUE			0X01		/* 按键值 */
#define INVAKEY				0XFF		/* 无效的按键值 */
#define KEY_NUM				1			/* 按键数量 	*/

struct irq_keydesc{
	int gpio;       /* 按键对应的GPIO编号 */
	int irqnum;     /* 按键对应的中断号 */
	unsigned char value;  /* 按键值 */
    char name[10]; /* 按键名称 */
    irqreturn_t (*handler)(int, void *); /* 中断处理函数 */
};
/* imx6ull 按键设备描述结构体 */
struct imx6ull_irq_dev{
	dev_t devid;			/* 设备号 	 */
	struct cdev cdev;		/* cdev 	*/
	struct class *class;	/* 类 		*/
	struct device *device;	/* 设备 	 */
	int major;				/* 主设备号	  */
	int minor;				/* 次设备号   */
	struct device_node	*nd; /* 设备节点 */
	atomic_t keyvalue;			/* 按键值 	*/
    atomic_t releasekey;    /* 标记按键是否按下 */
	struct timer_list timer;		/*定时器*/
	struct irq_keydesc irqkeydesc[KEY_NUM]; /* 按键描述数组 */
	unsigned char curkeynum;    /* 当前按键号 */
};

struct imx6ull_irq_dev imx6ullirq;	/* imx6ull按键设备 */

static int imx6ull_open(struct inode *inode, struct file *filp)
{
	filp->private_data = &imx6ullirq; /* 设置私有数据 */
	return 0;
}

static ssize_t imx6ull_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
	//printk("imx6ull_read\r\n");
	int ret = 0;
	unsigned char keyvalue = 0;
	unsigned char releasekey = 0;
	struct imx6ull_irq_dev *dev =  (struct imx6ull_irq_dev *)filp->private_data;
	keyvalue = atomic_read(&dev->keyvalue);
	releasekey = atomic_read(&dev->releasekey);

	if (releasekey == 1)
	{
		if (keyvalue & 0x80)
		{
			printk("keyvalue = %#X, releasekey = %d\r\n", keyvalue & ~0x80, releasekey);
			keyvalue &= ~0x80;
			ret = copy_to_user(buf,&keyvalue,sizeof(keyvalue));
			printk("keyvalue = %#X\r\n", keyvalue);
			if (ret < 0)
			{
				printk("copy to user failed!\r\n");
			}

		}else{
			goto date_error;
		}
		atomic_set(&dev->releasekey, 0);/* 按下标志清零 */
	}else {
		goto date_error;
	}
	return 0;
date_error:
	return -EINVAL;

}

static struct file_operations imx6uirq_fops = {
	.owner = THIS_MODULE,
	.open = imx6ull_open,
	.read = imx6ull_read,
};
static void timer_function(unsigned long arg)
{
	printk("timer_function\r\n");
	unsigned char value;
	unsigned char num = 0;
	struct irq_keydesc *keydesc;
	struct imx6ull_irq_dev *dev = (struct imx6ull_irq_dev *)arg;

	num = dev->curkeynum;
	keydesc = &dev->irqkeydesc[num];

	value = gpio_get_value(keydesc->gpio);
	if (value == 0)		/* 按键按下 */
	{
		atomic_set(&dev->keyvalue, keydesc->value);
	}
	else			// 按键释放
	{
		atomic_set(&dev->keyvalue, 0x80 | keydesc->value);
		atomic_set(&dev->releasekey, 1);
	}

}
static irqreturn_t button_irq_handler(int irq, void *dev_id)
{
	printk("button_irq_handler\r\n");
	struct imx6ull_irq_dev *dev = (struct imx6ull_irq_dev *)dev_id;
	dev->curkeynum = 0;
	dev->timer.data = (volatile long)dev_id;
	mod_timer(&dev->timer, jiffies + msecs_to_jiffies(10));	//10ms定时
	return IRQ_RETVAL(IRQ_HANDLED);
}
static int keyio_init(void)
{
	unsigned char i = 0;
	int ret = 0;
	imx6ullirq.nd = of_find_node_by_path("/key");
	if (imx6ullirq.nd == NULL)
	{
		printk("key not find..\r\n");
		return -EINVAL;
	}
	for (i = 0; i < KEY_NUM;i++){
		imx6ullirq.irqkeydesc[i].gpio = of_get_named_gpio(imx6ullirq.nd, "key-gpios", i);
		if (imx6ullirq.irqkeydesc[i].gpio < 0)
		{
			printk("can't find key-gpio\r\n");
			return -EINVAL;
		}
	}
	/* 初始化key所使用的IO，并且设置成中断模式 */
	for (i = 0; i < KEY_NUM; i++) {
		memset(imx6ullirq.irqkeydesc[i].name, 0, sizeof(imx6ullirq.irqkeydesc[i].name));	/* 缓冲区清零 */
		sprintf(imx6ullirq.irqkeydesc[i].name, "KEY%d", i);		/* 组合名字 */
		gpio_request(imx6ullirq.irqkeydesc[i].gpio, imx6ullirq.irqkeydesc[i].name);
		gpio_direction_input(imx6ullirq.irqkeydesc[i].gpio);
		imx6ullirq.irqkeydesc[i].irqnum = irq_of_parse_and_map(imx6ullirq.nd, i);
#if 0
		imx6uirq.irqkeydesc[i].irqnum = gpio_to_irq(imx6uirq.irqkeydesc[i].gpio);
#endif
		printk("key%d:gpio=%d, irqnum=%d\r\n",i, imx6ullirq.irqkeydesc[i].gpio,imx6ullirq.irqkeydesc[i].irqnum);
	}
			/* 申请中断 */
	imx6ullirq.irqkeydesc[0].handler = button_irq_handler;
	imx6ullirq.irqkeydesc[0].value = KEY0VALUE;
	
	for (i = 0; i < KEY_NUM; i++) {
		ret = request_irq(imx6ullirq.irqkeydesc[i].irqnum, imx6ullirq.irqkeydesc[i].handler, 
		                 IRQF_TRIGGER_FALLING|IRQF_TRIGGER_RISING, 
						 imx6ullirq.irqkeydesc[i].name, &imx6ullirq);
		if(ret < 0){
			printk("irq %d request failed!\r\n", imx6ullirq.irqkeydesc[i].irqnum);
			return -EFAULT;
		}
	}
		//初始化定时器
	init_timer(&imx6ullirq.timer);

	imx6ullirq.timer.function = timer_function;
	return 0;
}
static int __init imx6ull_irq_init(void)
{
	printk("imx6ullirq init...\r\n");
	/* 1、构建设备号 */
	if (imx6ullirq.major) {
		imx6ullirq.devid = MKDEV(imx6ullirq.major, 0);
		register_chrdev_region(imx6ullirq.devid, IMX6ULLIRQ_CNT, IMX6ULLIRQ_NAME);
	} else {
		alloc_chrdev_region(&imx6ullirq.devid, 0, IMX6ULLIRQ_CNT, IMX6ULLIRQ_NAME);
		imx6ullirq.major = MAJOR(imx6ullirq.devid);
		imx6ullirq.minor = MINOR(imx6ullirq.devid);
	}

	/* 2、注册字符设备 */
	cdev_init(&imx6ullirq.cdev, &imx6uirq_fops);
	cdev_add(&imx6ullirq.cdev, imx6ullirq.devid, IMX6ULLIRQ_CNT);

	/* 3、创建类 */
	imx6ullirq.class = class_create(THIS_MODULE, IMX6ULLIRQ_NAME);
	if (IS_ERR(imx6ullirq.class)) {
		return PTR_ERR(imx6ullirq.class);
	}

	/* 4、创建设备 */
	imx6ullirq.device = device_create(imx6ullirq.class, NULL,
		imx6ullirq.devid, NULL, IMX6ULLIRQ_NAME);
	if (IS_ERR(imx6ullirq.device)) {
		return PTR_ERR(imx6ullirq.device);
	}

	/* 5、初始化按键 */
	atomic_set(&imx6ullirq.keyvalue, INVAKEY);
	atomic_set(&imx6ullirq.releasekey, 0);

	keyio_init();
	return 0;
}



static void __exit imx6ull_irq_exit(void)
{
    unsigned int i = 0;
	printk("imx6ullirq exit...\r\n");
	/* 删除定时器 */
	del_timer_sync(&imx6ullirq.timer);	/* 删除定时器 */
    /* 释放中断 */
    for (i = 0; i < KEY_NUM; i++) {
        free_irq(imx6ullirq.irqkeydesc[i].irqnum, &imx6ullirq);
        gpio_free(imx6ullirq.irqkeydesc[i].gpio);
    }
    cdev_del(&imx6ullirq.cdev);
    unregister_chrdev_region(imx6ullirq.devid, IMX6ULLIRQ_CNT);
    device_destroy(imx6ullirq.class, imx6ullirq.devid);
    class_destroy(imx6ullirq.class);
}


module_init(imx6ull_irq_init);
module_exit(imx6ull_irq_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("wxy");



